using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SecurityModule.Entity;
using SBCommon;
using System.Collections;
using System.Collections.Generic;

namespace SecurityModule.Web.App_Code
{
    public class TreeViewHelper
    {

        public static void InitTreeView<T>(TreeView treeView, List<T> lists) where T : ITreeNodeAdaptor
        {
            treeView.Nodes.Clear();
            foreach (T t in lists)
            {
                if (string.IsNullOrEmpty(t.ItemTextColoumn))
                {
                    continue;
                }
                TreeNode node = new TreeNode(t.ItemTextColoumn, t.ItemValueColoumn);
                TreeViewRecursion<T>(node, lists, t.ItemValueColoumn);
                treeView.Nodes.Add(node);
            }
        }

        static void TreeViewRecursion<T>(TreeNode node, List<T> lists, string parentId) where T : ITreeNodeAdaptor
        {
            List<T> childLists= lists.FindAll(
                delegate(T t)
                {
                    return t.ParentColoumn == parentId;
                }
                );
            if (childLists.Count == 0)
            {
                return;
            }

            foreach (T t in childLists)
            {
                TreeNode childNode = new TreeNode(t.ItemTextColoumn, t.ItemValueColoumn);
                node.ChildNodes.Add(childNode);

                TreeViewRecursion(childNode, lists, t.ItemValueColoumn);
            }
           
        }

    }
}
